// Learn cc.Class:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const GameDataManger = require("../GameDataManger");
const UIPanelManager = require("../UIPanelManager");

cc.Class({
    extends: cc.Component,

    properties: {
        win:{     
            default:null,
            type:cc.Node
        },

        fail:{     
            default:null,
            type:cc.Node
        },

        desLab:{     
            default:null,
            type:cc.Label
        },

        matchContent:{     
            default:null,
            type:cc.Node,
        },

        nextBtn:{     
            default:null,
            type:cc.Node,
        },
    },

    onEnable(){
        this.refresh();
    },

    refresh(){
        this.win.active = GameDataManger.resultNum == 1;
        this.fail.active = GameDataManger.resultNum == 2;

        if(GameDataManger.gameType == 1){
            //匹配
            this.matchContent.active = true;
            this.desLab.string = "";
            this.nextBtn.active = false;

            for (let index = 0; index < GameDataManger.gradConfig.length; index++) {
                const element = GameDataManger.gradConfig[index];
                if (element.id == GameDataManger.selfGrade){
                    this.loadIamge(this.matchContent.getChildByName("gradeIcon"), element.icon)
                    this.matchContent.getChildByName("gradeLab").getComponent(cc.Label).string = element.name + ": " + GameDataManger.selfScore;
                    if (GameDataManger.resultNum == 1){
                        this.matchContent.getChildByName("winLab").getComponent(cc.Label).string = "+20"
                        this.matchContent.getChildByName("failLab").getComponent(cc.Label).string = ""
                        GameDataManger.selfScore+=20

                    }else{
                        this.matchContent.getChildByName("winLab").getComponent(cc.Label).string = ""
                        this.matchContent.getChildByName("failLab").getComponent(cc.Label).string = "-20"

                        GameDataManger.selfScore = Math.max(0, GameDataManger.selfScore-20)
                    }
                    break;
                }
            }
        }else{
            this.matchContent.active = false;
            this.nextBtn.active = false;

            if (GameDataManger.gameType == 2){
                //ai
                let des = GameDataManger.passTip[Math.min(GameDataManger.passTip.length-1, GameDataManger.curLevel)]
                this.desLab.string = GameDataManger.resultNum == 1? des:"再接再厉!";
                if (GameDataManger.curLevel < 2){
                    this.nextBtn.active = GameDataManger.resultNum == 1;
                    if(GameDataManger.resultNum == 1){
                        GameDataManger.curLevel++;
                    }
                }
            }else{
                //好友
                this.desLab.string = GameDataManger.resultNum == 1? "恭喜你拿下本局!!":"你被对方击败了。";
            }
        }

         cc.systemEvent.emit('saveUserData');
    },

    loadIamge(image, iconName){
        let url = 'imags/grades/' + iconName
        cc.resources.load(url, cc.SpriteFrame, (err, asset) => {
            if (err){
                console.log("资源加载失败")
                return;
            }
            image.getComponent(cc.Sprite).spriteFrame = asset;
        });
    },

    btnClick(event,str){
        if (str == "share"){
            //todo 开始游戏
            //UIPanelManager.open(UIPanelManager.matchNode);
        }else if (str == "back"){
            UIPanelManager.close(this.node);
            // cc.director.loadScene("main");
        }
        else if (str == "next"){
            if (GameDataManger.gold < 5){
                cc.systemEvent.emit('showTip', "金币不足");
                return;
            }
            GameDataManger.gold-=5;
            GameDataManger.gameType = 2;
            var ranIndex = Math.floor(Math.random()*2);
            cc.systemEvent.emit('sendMessage', 1002, [0,true]);
            cc.systemEvent.emit('saveUserData');
            cc.systemEvent.emit('connectAi', GameDataManger.curLevel);
            UIPanelManager.close(this.node);
        }
    },
});
